import 'package:flutter_base_library/index.dart';
import 'package:get/get.dart';
import 'package:safe_manage/config/my_app_routes.dart';
import 'package:safe_manage/models/index.dart';
import '../../../config/my_app_data.dart';

class MySplashController extends GetxController {
  var showLoginButton = false.obs;
  @override
  void onReady() {
    super.onReady();
    // MyAppData.instance.initData();
    // Get.offAllNamed(AppRoutes.baseTabBar);
    KVDataUtils kvDataUtils = KVDataUtils.getInstance();
    String? realm = kvDataUtils.getString("realm");
    String? token = kvDataUtils.getString("token");
    String? username = kvDataUtils.getString("username");
    String? password = kvDataUtils.getString("password");
    if (realm != null && realm.isNotEmpty) {
      AppData.instance.baseUrl = realm;
      if (token != null && token.isNotEmpty) {
        HttpUtils.post(
          "/safety-web/sysUser/login",
          body: {
            "parameter": {
              "dlmc": username,
              "dlmm": password,
            }
          },
          success: (result) {
            MyAppData.instance.currentUser = User.fromJson(result['data']);
            AppData.instance.token = result['data']['dlmm'];
            Get.offAllNamed(MyAppRoutes.baseTabBar);
            showLoginButton.value = false;
          },
          fail: (errorMsg) {
            showLoginButton.value = true;
          },
        );
      } else {
        showLoginButton.value = true;
      }
    } else {
      showLoginButton.value = true;
    }
  }
}
